#include "evaluater.h"

// TODO this is really a very :-) simple evaluation
int Evaluater::getScore() {
    if (WHITE_TO_MOVE == board_.sideToMove) return -board_.material;
    if (BLACK_TO_MOVE == board_.sideToMove) return board_.material;
}

// calc complete score from plain material (white has +)
int Evaluater::calcBoardScore() {
    int wMaterial = PAWN_VALUE   * Bitutils::count(board_.wP) +
                    KNIGHT_VALUE * Bitutils::count(board_.wN) +
                    BISHOP_VALUE * Bitutils::count(board_.wB) +
                    ROOK_VALUE   * Bitutils::count(board_.wR) +
                    QUEEN_VALUE  * Bitutils::count(board_.wQ);

    //wMaterial += 30 * Bitutils::count(ROOK_VALUE_PATTERN & board_.wR);
    //wMaterial += 50 * Bitutils::count(KNIGHT_VALUE_PATTERN & board_.wN);

    int bMaterial = PAWN_VALUE   * Bitutils::count(board_.wP) +
                    KNIGHT_VALUE * Bitutils::count(board_.wN) +
                    BISHOP_VALUE * Bitutils::count(board_.wB) +
                    ROOK_VALUE   * Bitutils::count(board_.wR) +
                    QUEEN_VALUE  * Bitutils::count(board_.wQ);

    //bMaterial += 30 * Bitutils::count(ROOK_VALUE_PATTERN & board_.bR);
    //bMaterial += 50 * Bitutils::count(KNIGHT_VALUE_PATTERN & board_.bN);


    board_.material = wMaterial - bMaterial;
    qDebug() << "Board score calculated";

    if (wMaterial == 0) {
        qDebug() << "White has lost all pieces";
        exit(1);
    }
    if (bMaterial == 0) {
        qDebug() << "Black has lost all pieces";
        exit(1);
    }
}
